package cn.spring.ly.controller;

import java.io.IOException;
import java.util.List;

import javax.jms.JMSException;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.solr.client.solrj.SolrServerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import cn.spring.ly.entity.Category;
import cn.spring.ly.entity.Goods;
import cn.spring.ly.page.PageInfo;
import cn.spring.ly.result.AjaxResult;
import cn.spring.ly.service.CategoryService;
import cn.spring.ly.service.GoodsService;
import cn.spring.ly.service.MessageService;

@Controller
@RequestMapping("/sys/goods")
public class GoodsController {

	@Autowired
	GoodsService goodsService;

	@Autowired
	CategoryService categoryService;

	@Autowired
	MessageService messageService;
	
	@RequiresPermissions("goods:listsolr")
	@RequestMapping("/listsolr")
	public String list(Model model,
			@RequestParam(defaultValue = "1", required = false) Integer pageNo,
			String queryInput) throws SolrServerException, IOException {
		PageInfo<Goods> pageInfo = goodsService.selectBySolrPage(pageNo, queryInput);
		model.addAttribute("pageInfo", pageInfo);
		return "goods_list_solr";
	}

	@RequiresPermissions("goods:list")
	@RequestMapping("/list")
	public String list(Model model,
			@RequestParam(defaultValue = "1", required = false) Integer pageNo,
			String queryInput,
			Integer catePid) {
		PageInfo<Goods> pageInfo = new PageInfo<Goods>(pageNo, 5);
		pageInfo = goodsService.selectByPage(pageInfo, queryInput,catePid);
		model.addAttribute("pageInfo", pageInfo);
		
		QueryWrapper<Category> queryWrapper = new QueryWrapper<Category>();
		queryWrapper.eq(true,  Category.PARENT_ID, 0);
		List<Category> cateList = categoryService.list(queryWrapper);
		model.addAttribute("cateList", cateList);
		
		model.addAttribute("queryInput", queryInput);
		model.addAttribute("catePid", catePid);
		return "goods_list";
	}
	@RequiresPermissions("goods:add")
	@RequestMapping("/add")
	public String addGood() throws JMSException{

		
		goodsService.clearGoods();
		messageService.sendGoodsMessage("商品添加成功");
		messageService.sendUserMessage();
		return "goods_list";
	}

	@RequiresPermissions("goods:del")
	@RequestMapping("/del")
	@ResponseBody
	public AjaxResult delGood(Integer id)
	{
//		Goods entity = new Goods();
//		entity.setId(id);
//		
//		entity.setIsDelete(true);
		boolean f = goodsService.removeById(id);
		if(f){
			goodsService.clearGoods();
			return AjaxResult.succes("删除成功!");
		}
		return AjaxResult.error("删除失败!");
	}
}
